package com.oauth.controller;

import com.oauth.mapper.OpenClientMapper;
import com.oauth.model.OpenToken;
import com.oauth.service.OauthAuthorizeService;
import com.oauth.service.OpenClientService;
import com.oauth.service.OpenTokenService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.oltu.oauth2.as.issuer.MD5Generator;
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl;
import org.apache.oltu.oauth2.as.request.OAuthAuthzRequest;
import org.apache.oltu.oauth2.as.response.OAuthASResponse;
import org.apache.oltu.oauth2.as.validator.AuthorizationCodeValidator;
import org.apache.oltu.oauth2.as.validator.CodeValidator;
import org.apache.oltu.oauth2.as.validator.TokenValidator;
import org.apache.oltu.oauth2.common.OAuth;
import org.apache.oltu.oauth2.common.error.OAuthError;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.OAuthResponse;
import org.apache.oltu.oauth2.common.message.types.ResponseType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * 授权控制器
 * code模式
 * token模式
 * Created by yangjianbo on 2016/9/24.
 */
@Controller
@RequestMapping("oauth")
public class AuthorizeController {

    @Autowired
    private OauthAuthorizeService oauthAuthorizeService;

    //http://localhost:8080/authorize?response_type=code&client_id=111&redirect_uri=http://www.baidu.com
    @RequestMapping(value = "/authorize",method = RequestMethod.GET)
    public Object authorize(HttpServletRequest request) throws OAuthProblemException, OAuthSystemException, URISyntaxException {
        //构建OAuth 授权请求
        OAuthAuthzRequest oauthRequest = new OAuthAuthzRequest(request);
        if(ResponseType.CODE.toString().equals(oauthRequest.getResponseType())){
            //code类型
            CodeValidator codeValidator=new CodeValidator();
            codeValidator.performAllValidations(request);

            String code=oauthAuthorizeService.authorizeByCode(
                    oauthRequest.getClientId(),oauthRequest.getRedirectURI(),oauthRequest.getParam(OAuth.OAUTH_SCOPE));
            //进行OAuth响应构建
            OAuthASResponse.OAuthAuthorizationResponseBuilder builder =
                    OAuthASResponse.authorizationResponse(request,
                            HttpServletResponse.SC_FOUND);
            builder.setCode(code);
            //构建响应
            final OAuthResponse response = builder.location(oauthRequest.getRedirectURI()).buildQueryMessage();
            HttpHeaders headers = new HttpHeaders();
            headers.setLocation(new URI(response.getLocationUri()));
            return new ResponseEntity(headers, HttpStatus.valueOf(response.getResponseStatus()));

        }else if(ResponseType.TOKEN.toString().equals(oauthRequest.getResponseType())){
            //token类型
            throw  OAuthProblemException.error("目前不支持token类型--简化模式");
        }else {
            throw OAuthProblemException.error("不支持该response类型");
        }
    }

    @RequestMapping(value = "/test",produces = "application/json",method = RequestMethod.GET)
    @ResponseBody
    Map test(){
        Map map=new HashMap<>();
        map.put("hess",11);
        map.put("lese",22);
        return map;
    }


}
